<style scoped lang="less">
	@import "active-info.less";
</style>
<template>
	<Card class="active-info-container">
		<div class="active-store-wrap">
			<span class="active-store-name-title">店铺</span>
			<router-link :to="storeLink">{{formItem.store_name}}</router-link>
		</div>
		
		<Form ref="myForm" :model="formItem" :rules="ruleValidate" :label-width="100">
			<FormItem label="活动标题" prop="title">
				<Input :disabled="showOnly" v-model="formItem.title" placeholder="请输入活动标题" style="width:40vw"/>
			</FormItem>
			<FormItem label="推荐语" prop="recommendDesc">
				<div v-for="(item) in formItem.recommendDesc">
					<Input :show-word-limit="true" :maxlength="26" :disabled="showOnly" v-model="item.content"
					       placeholder="请输入推荐语" style="width:460px"/>
				</div>
				<div>
					<span v-if="!showOnly" class="btn-add" @click="addRecommend"><Icon type="md-add"/></span>
				</div>
			</FormItem>
			<FormItem label="分成方式">
				<Select :disabled="showOnly" v-model="formItem.distribution_type" style="width:100px;margin-right: 10px;">
					<Option :value="0">按百分比</Option>
					<Option :value="1">按固定金额</Option>
				</Select>
				<InputNumber
					:disabled="showOnly"
					v-if="formItem.distribution_type===0"
					:max="100"
					:min="0"
					v-model="formItem.distribution_percentage"
					:formatter="value => `${value}%`"
					:parser="value => value.replace('%', '')"></InputNumber>
				<InputNumber
					v-else
					:disabled="showOnly"
					:min="0"
					v-model="formItem.distribution_money"
				></InputNumber>
			</FormItem>
			<FormItem label="退款比例">
				<InputNumber
					:max="100"
					:min="0"
					:disabled="showOnly"
					v-model="formItem.refund_percentage"
					:formatter="value => `${value}%`"
					:parser="value => value.replace('%', '')"></InputNumber>
			</FormItem>
			<FormItem label="支付方式">
				<Select :disabled="showOnly" v-model="formItem.pay_type" style="width:100px">
					<template v-for="item in pay_types">
						<Option :value="item.value">{{item.name}}</Option>
					</template>
				</Select>
			</FormItem>
			<FormItem label="发货方式">
				<Select :disabled="showOnly" v-model="formItem.transport_type" style="width:100px">
					<template v-for="item in transport_types">
						<Option :value="item.value">{{item.name}}</Option>
					</template>
				</Select>
			</FormItem>
			
			<FormItem label="显示在前台">
				<Select :disabled="showOnly" v-model="formItem.show" style="width:100px">
					<template v-for="item in active_shows">
						<Option :value="item.value">{{item.name}}</Option>
					</template>
				</Select>
			</FormItem>
			<FormItem label="分类">
				<Select :disabled="showOnly" v-model="formItem.category" style="width:100px">
					<Option :value="-1">不选</Option>
					<template v-for="item in categoryList">
						<Option :value="item.id">{{item.title}}</Option>
					</template>
				</Select>
			</FormItem>
			<FormItem label="活动类型" prop="type">
				<Select :disabled="showOnly||formItem.id!=null" v-model="formItem.type" style="width:100px">
					<Option :value="-1">请选择</Option>
					<template v-for="item in active_types">
						<Option :value="item.value">{{item.name}}</Option>
					</template>
				</Select>
				<Tooltip>
					<div slot="content">
						<p>活动类型一旦保存，就无法再修改。</p>
					</div>
					<Icon type="md-alert"/>
				</Tooltip>
			</FormItem>
			<FormItem label="套餐">
				<Tooltip>
					<div slot="content">
						<p>一个活动最少需要一个套餐，</p>
						<p>多套餐会在详情中展示，</p>
						<p>单套餐默认只展示一个</p>
					</div>
					<Icon type="md-alert"/>
				</Tooltip>
				<div class="stock-wrap">
					<div class="stock-item" v-for="(item, index) in formItem.sku" :key="index">
						<span v-if="!showOnly" class="stock-item-del" @click="delSku(index)">删除</span>
						<span v-if="!showOnly" class="stock-item-edit" @click="editSku(index)">编辑</span>
						<div class="stock-item-title">{{'套餐'+index}}</div>
						<FormItem label="名称">
							<Input :disabled="true" :readonly="true" v-model="item.title"
							       style="width:180px"/>
						</FormItem>
						<FormItem label="原价">
							<Input :disabled="true" :readonly="true" v-model="item.original_price"
							       style="width:60px"/>
						</FormItem>
						<FormItem label="最低价">
							<Input :disabled="true" v-model="item.lowest_price" style="width:60px"/>
						</FormItem>
						<FormItem label="VIP优惠">
							<Input :disabled="true" v-model="item.vip_discount" style="width:60px"/>
						</FormItem>
						<FormItem label="推荐赏金">
							<Input :disabled="true" v-model="item.reward" style="width:60px"/>
						</FormItem>
						<FormItem label="数量">
							<Input :disabled="true" v-model="item.stock" style="width:120px"/>
						</FormItem>
						<FormItem label="初始销量">
							<Input :disabled="true" v-model="item.init_sales" laceholder=""
							       style="width:120px"></Input>
						</FormItem>
						<FormItem label="限购数量">
							<Input :disabled="true" v-model="item.limit_buy_count" style="width:180px"/>
						</FormItem>
					</div>
					<!-- 砍价活动只允许有一个套餐          -->
					<div v-if="!(showOnly||(formItem.type===GLOBAL.ACTIVE_TYPE_KJ&&formItem.sku.length>=1))"
					     @click="showAddSkuModel"
					     class="stock-add">添加
					</div>
				</div>
			</FormItem>
			
			<FormItem label="起止日期" prop="date">
				<DatePicker :disabled="showOnly" v-model="formItem.date" type="datetimerange" format="yyyy-MM-dd HH:mm"
				            placeholder="选择起止日期" style="width:260px"></DatePicker>
			</FormItem>
			<FormItem label="使用日期" prop="use_limit_date">
				<DatePicker :disabled="showOnly" v-model="formItem.use_limit_date"
				            type="datetimerange" format="yyyy-MM-dd HH:mm" placeholder="选择起止日期"
				            style="width:260px"></DatePicker>
			</FormItem>
			<FormItem label="限制参与地区">
				<AreaCascader v-model="formItem.region" level="2" :disabled="showOnly" style="width:350px"/>
			</FormItem>
			<FormItem label="初始浏览数" prop="share_content">
				<InputNumber :min="0" :precision="0" :disabled="showOnly" v-model="formItem.init_views" laceholder=""
				             style="width:120px"></InputNumber>
			</FormItem>
			<FormItem label="活动照片" prop="banners">
				<SelectImg :max-size="2*1024" v-model="formItem.banners" :format="'img'" @on-selected-imgs="bannerImgSelected"
				           :canOp="!showOnly"
				           :limit="6"
				           :initList="formItem.banners"></SelectImg>
			</FormItem>
			<FormItem label="活动视频" prop="video">
				<SelectImg v-model="formItem.video" :max-size="2*10*1024" :format="'video'" @on-selected-imgs="videoSelected"
				           :canOp="!showOnly"
				           :limit="1"
				           :initList="formItem.video"></SelectImg>
			</FormItem>
			<FormItem label="分享标题" prop="share_title">
				<Input :disabled="showOnly" v-model="formItem.share_title" placeholder="请输入分享标题" style="width:40vw"/>
			</FormItem>
			<FormItem label="分享描述" prop="share_content">
				<Input :disabled="showOnly" v-model="formItem.share_content" placeholder="请输入分享描述"/>
			</FormItem>
			<FormItem label="分享照片" prop="share_img">
				<SelectImg ref="shareImg" v-model="formItem.share_img" @on-selected-imgs="shareImgSelected" :canOp="!showOnly"
				           :limit="1"
				           :initList="formItem.share_img"></SelectImg>
			</FormItem>
			<FormItem label="海报照片" prop="share_img_bg">
				<SelectImg ref="share_img_bg" v-model="formItem.share_img_bg" @on-selected-imgs="shareImgBgSelected"
				           :canOp="!showOnly"
				           :limit="1"
				           :initList="formItem.share_img_bg"></SelectImg>
			</FormItem>
			<FormItem label="购买须知" prop="notice_content">
				<MyUeditor :readonly="showOnly" v-model="formItem.notice_content" style="width:80%"></MyUeditor>
			</FormItem>
			<FormItem label="活动详情" prop="content">
				<MyUeditor :readonly="showOnly" v-model="formItem.content" style="width:80%"></MyUeditor>
				<Button type="text" @click="preview" style="margin-right: 8px">预览</Button>
			</FormItem>
		</Form>
		<div class="active-info-btn-wrap" v-if="!showOnly">
			<Button type="text" @click="doCancel" style="margin-right: 8px">取消</Button>
			<Button type="primary" @click="submit" :loading="submitLoading">提交</Button>
		</div>
		<Modal v-if="skuFormItem!=null" v-model="modalSetting.show" width="668" :styles="{top: '30px'}"
		       @on-visible-change="doCancel">
			<Form ref="skuForm" :rules="skuRuleValidate" :model="skuFormItem" :label-width="80">
				<Tabs value="0">
					<TabPane label="套餐设置" name="0">
						<FormItem label="名称" prop="title">
							<Input :disabled="showOnly" :readonly="showOnly" v-model="skuFormItem.title" placeholder="请输入名称"
							       style="width:180px"/>
						</FormItem>
						<FormItem label="原价" prop="original_price">
							<InputNumber :min="0.01" :disabled="showOnly" v-model="skuFormItem.original_price" laceholder="请输入原价"
							             style="width:120px"></InputNumber>
						</FormItem>
						<FormItem label="最低价" prop="lowest_price">
							<InputNumber :min="0.01" :disabled="showOnly" v-model="skuFormItem.lowest_price" laceholder="请输入最低价"
							             style="width:120px"></InputNumber>
						</FormItem>
						<FormItem label="VIP优惠" prop="lowest_price">
							<InputNumber :min="0" :disabled="showOnly" v-model="skuFormItem.vip_discount" laceholder="0为不优惠"
							             style="width:120px"></InputNumber>
						</FormItem>
						<FormItem label="推荐赏金" prop="lowest_price">
							<InputNumber :min="0" :disabled="showOnly" v-model="skuFormItem.reward" laceholder="0没有赏金"
							             style="width:120px"></InputNumber>
						</FormItem>
						<FormItem label="数量" :min="0" prop="stock">
							<InputNumber :min="0" :disabled="showOnly" v-model="skuFormItem.stock" laceholder="请输入数量"
							             style="width:120px"></InputNumber>
						</FormItem>
						<FormItem label="初始销量" prop="init_sales">
							<InputNumber :min="0" :precision="0" :disabled="showOnly" v-model="skuFormItem.init_sales" laceholder=""
							             style="width:120px"></InputNumber>
						</FormItem>
						<FormItem label="限购数量" prop="limit_buy_count">
							<InputNumber :min="0" :disabled="showOnly" v-model="skuFormItem.limit_buy_count" laceholder="请输入数量,0为不限制"
							             style="width:60px"></InputNumber>
						</FormItem>
					</TabPane>
					<TabPane v-if="formItem.type===GLOBAL.ACTIVE_TYPE_KJ" label="砍价规则设置" name="1">
						<div class="rule-wrap">
							<div class="rule-title">
								<span class="rule-title-item">小于等于当前价格</span>
								<span class="rule-title-item">随机砍价区间</span>
								<span class="rule-title-item">操作</span>
							</div>
							<div class="rule-item" v-for="(item,index) in  skuFormItem.rule_data.rule">
                <span class="rule-item-content">
                  <InputNumber :precision="2" :min="index===0?skuFormItem.original_price:skuFormItem.lowest_price"
                               :max="skuFormItem.original_price"
                               v-model="item.price" size="small"></InputNumber>
                </span>
								<span class="rule-item-content">
                  <InputNumber v-model="item.start_price" :precision="2" :min="0.01" size="small"></InputNumber> 至
                  <InputNumber v-model="item.end_price" :precision="2" :min="item.start_price"
                               size="small"></InputNumber>
                </span>
								<span class="rule-item-content">
                  <Button size="small" @click="delRule(index)">删除</Button>
                </span>
							</div>
							<Button type="info" class="btn-add-rule" @click="addRule" size="small">添加一条</Button>
							<div class="rule-desc">粉丝帮忙砍价的随机规则:<br/>1、第一条价格必须是等于原价。<br/>2、小于等于当前价格从大到小填写。</div>
						
						</div>
					
					</TabPane>
				</Tabs>
			
			</Form>
			<div slot="footer">
				<Button type="text" @click="cancel" style="margin-right: 8px">取消</Button>
				<Button type="primary" @click="addSku" :loading="modalSetting.loading">确定</Button>
			</div>
		</Modal>
	</Card>
</template>
<script>
  import MyUeditor from '_c/myueditor'
  import SelectImg from '../../components/select-img'
  import AreaCascader from '../../components/area'
  import {save, get} from '@/api/active'
  import {getList} from '@/api/category'
  import {get as getStoreData} from '@/api/stores'

  export default {
    name: 'active-info',
    components: {
      SelectImg,
      AreaCascader,
      MyUeditor
    },
    data() {
      return {
        active_shows: [],
        active_types: [],
        pay_types: [],
        transport_types: [],
        categoryList: [],
        modalSetting: {
          show: false,
          loading: false
        },
        storeLink: '/stores/stores-edit/',
        showOnly: false,
        submitLoading: false,
        skuFormItem: {
          add: true,
          id: null,
          aid: null,
          title: '',
          original_price: 0,
          lowest_price: 0,
          init_sales: 0,
          stock: 0,
          vip_discount: 0,
          reward: 0,
          limit_buy_count: 0,
          rule_data: {id: null, rule: []}
        },
        formItem: {
          recommendDesc: [],
          video: null,
          id: null,
          sid: -1,
          type: -1,
          show: 0,
          category: null,
          store_name: null,
          init_views: 0,
          active: 0,
          title: '',
          pay_type: 0,
          distribution_type: 0,
          distribution_money: 0,
          distribution_percentage: 0,
          refund_percentage: 90,
          transport_type: 0,
          sku: [],
          date: [],
          use_limit_date: [],
          share_title: '',
          share_content: '',
          share_img: [],
          share_img_bg: [],
          banners: [],
          content: '',
          region: []
        },
        skuRuleValidate: {
          title: [
            {required: true, message: '标题不能为空', trigger: 'blur'}
          ],
          original_price: [
            {type: 'number', required: true, message: '原价不能为空', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (value === null || value === undefined || value === '' || (value + '').trim() === '') {
                  callback('请输入原价');
                } else if (Number(value) <= 0) {
                  callback('请输入大于0的数字');
                } else {
                  callback();
                }
              }
            }
          ],
          vip_discount: [
            {type: 'number', required: true, message: '优惠不能为空', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (value === null || value === undefined || value === '' || (value + '').trim() === '') {
                  callback('请输入抵扣');
                } else if (Number(value) < 0) {
                  callback('请输入大于等于0的数字');
                } else {
                  callback();
                }
              }
            }
          ],
          lowest_price: [
            {type: 'number', required: true, message: '价格最低不能低于0,且须为数字', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (value === null || value === undefined || value === '' || (value + '').trim() === '') {
                  callback('请输入价格最低');
                } else if (Number(value) <= 0) {
                  callback('请输入大于0的数字');
                } else if (Number(value) >= this.skuFormItem.original_price) {
                  callback('最低价不得高于原价');
                } else {
                  callback();
                }
              }
            }
          ],
          init_sales: [{type: 'number', required: true, message: '请填入整数', trigger: 'blur'}],
          limit_buy_count: [
            {type: 'number', required: true, message: '请填入整数', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (value === null || value === undefined || value === '' || (value + '').trim() === '') {
                  callback('请输入限制数量');
                } else if (Number(value) < 0) {
                  callback('请输入大于等于0的整数');
                } else if (Number(value) !== 0 && !(Number(value) % 1 === 0)) {
                  callback('请输入整数');
                } else {
                  callback();
                }
              }
            }
          ],
          stock: [
            {type: 'number', required: true, message: '请填写库存', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (value === null || value === undefined || value === '' || (value + '').trim() === '') {
                  callback('请输入库存');
                } else if (Number(value) < 1) {
                  callback('请输入大于0的整数');
                } else if (!(Number(value) % 1 === 0)) {
                  callback('请输入整数');
                } else {
                  callback();
                }
              }
            }
          ],
        },
        ruleValidate: {
          title: [
            {required: true, message: '标题不能为空', trigger: 'blur'}
          ],
          type: [
            {required: true, type: 'number', message: '请选择活动类型', trigger: 'change'},
            {
              validator: (rule, value, callback) => {
                if (value === -1) {
                  callback('请选择活动类型');
                } else {
                  callback();
                }
              }
            }
          ],
          date: [
            {required: true, type: 'array', message: '请选择日期', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (!value) {
                  callback('请选择日期');
                } else if (value.length === 0 || value[0] === '') {
                  callback('请选择日期');
                } else {
                  callback();
                }
              }
            }
          ],
          use_limit_date: [
            {required: true, type: 'array', message: '请选择日期', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (!value) {
                  callback('请选择日期');
                } else if (value.length === 0 || value[0] === '') {
                  callback('请选择日期');
                } else {
                  callback();
                }
              }
            }
          ],
          share_title: [
            {required: true, message: '请输入分享标题', trigger: 'blur'}
          ],
          share_content: [
            {required: true, message: '请输入分享描述', trigger: 'blur'}
          ],
          share_img: [
            {required: true, type: 'array', message: '请选择图片', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (!value) {
                  callback('请选择图片');
                } else if (value.length === 0 || value[0] === '') {
                  callback('请选择图片');
                } else {
                  callback();
                }
              }
            }
          ],
          share_img_bg: [
            {required: true, type: 'array', message: '请选择图片', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (!value) {
                  callback('请选择图片');
                } else if (value.length === 0 || value[0] === '') {
                  callback('请选择图片');
                } else {
                  callback();
                }
              }
            }
          ],
          banners: [
            {required: true, type: 'array', message: '请选择图片', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (!value) {
                  callback('请选择图片');
                } else if (value.length === 0 || value[0] === '') {
                  callback('请选择图片');
                } else {
                  callback();
                }
              }
            }
          ]
        }
      }
    },
    created() {
      this.pay_types = this.GLOBAL.PAY_TYPES;
      this.transport_types = this.GLOBAL.TRANSPORT_TYPES;
      this.active_types = this.GLOBAL.ACTIVE_TYPES;
      this.active_shows = this.GLOBAL.ACTIVE_SHOWS;
    },
    mounted() {
      this.init();
    },
    watch: {
      // 监听相同路由下参数变化的时候，从而实现异步刷新
      '$route'(to, from) {
        // 重置数据
        Object.assign(this.$data, this.$options.data());
        /** 初始化其他数据 */
        this.init();
      }
    },
    methods: {
      init() {
        this.showOnly = false;
        this.formItem.id = null;
        this.formItem.sid = null;
        let id = this.$route.params.id;
        let sid = this.$route.params.sid;
        let view = this.$route.params.view;
        if (sid !== undefined && sid !== null) {
          //创建活动
          this.formItem.sid = sid;
          this.storeLink += sid + '/1';
          this.getStoreData();
        } else {
          //编辑或预览活动
          if (id !== undefined && id !== null) {
            this.formItem.id = id;
            if (view !== undefined && view != 0) {
              this.showOnly = true;
            }
            this.getData();
          }
        }
        this.getCategoryList();
      },

      getCategoryList() {
        let vm = this;
        getList().then(rep => {
          vm.categoryList = rep.data.data;
        });
      },
      cancel() {
        this.modalSetting.show = false;
      },
      showAddSkuModel() {
        this.modalSetting.show = true;
        this.skuFormItem = {
          add: true,
          title: '',
          original_price: 0,
          lowest_price: 0,
          vip_discount: 0,
          reward: 0,
          stock: 0,
          init_sales: 0,
          limit_buy_count: 0,
          rule_data: {id: null, rule: []}
        };
      },
      addRecommend() {
        if (this.formItem.recommendDesc == null) {
          this.formItem.recommendDesc = [];
        }
        this.formItem.recommendDesc.push({content: ''});
      },
      addRule() {
        this.skuFormItem.rule_data.rule.push({price: 0, start_price: 0.01, end_price: 0.01});
      },
      delRule(index) {
        this.skuFormItem.rule_data.rule.splice(index, 1);
      },
      addSku() {
        let vm = this;
        vm.$refs['skuForm'].validate((valid) => {
            if (valid) {
              //如果是砍价，要校验砍价规则
              if (vm.formItem.type === vm.GLOBAL.ACTIVE_TYPE_KJ) {
                if (vm.skuFormItem.rule != null && vm.skuFormItem.rule.length > 0) {
                  let lastPrice = -1;
                  let done = true;
                  for (let i = 0; i < vm.skuFormItem.rule.length; i++) {
                    let v = vm.skuFormItem.rule[i];
                    if (v.start_price === 0 || v.end_price === 0) {
                      done = false;
                      vm.$Message.error('随便砍价区间价格不能为0');
                      break;
                    }
                    if (v.start_price > v.end_price) {
                      done = false;
                      vm.$Message.error('随便砍价区间起始价格不能高于截止价格');
                      break;
                    }
                    if (lastPrice === -1) {
                      if (v.price !== vm.skuFormItem.original_price) {
                        v.price = vm.skuFormItem.original_price;
                      }
                      lastPrice = v.price;
                    } else {
                      if (v.price >= lastPrice) {
                        done = false;
                        vm.$Message.error('砍价规则后一条的价格必须小于前一条的价');
                        break
                      }
                      lastPrice = v.price;
                    }
                  }
                  if (!done) {
                    return;
                  }
                }
              }
              if (vm.skuFormItem.id == null && vm.skuFormItem.add) {
                vm.formItem.sku.push(vm.skuFormItem);
              }
              this.modalSetting.show = false;
            }
          }
        );
      },
      editSku(index) {
        this.modalSetting.show = true;
        this.skuFormItem = this.formItem.sku[index];
        this.skuFormItem.add = false;
      },
      delSku(index) {
        if (this.formItem.sku.length > 1) {
          this.formItem.sku.splice(index, 1);
        } else {
          this.$Message.info('最少需要一个套餐');
        }
      },
      getStoreData() {
        let vm = this;
        getStoreData(vm.formItem.sid).then(rep => {
          vm.formItem.store_name = rep.data.data.name;
        }, error => {
        })
      },
      getData() {
        let vm = this;
        get(vm.formItem.id).then(rep => {
          vm.setInfo(rep.data.data);
        }, error => {
        })
      },
      setInfo(data) {

        let vm = this;
        vm.formItem.sid = data.sid;
        this.storeLink += data.sid + '/1';
        vm.formItem.store_name = data.store_name;
        vm.formItem.id = data.id;
        vm.formItem.type = data.type;
        vm.formItem.distribution_type = data.distribution_type;
        vm.formItem.distribution_money = data.distribution_money;
        vm.formItem.distribution_percentage = data.distribution_percentage;
        vm.formItem.refund_percentage = data.refund_percentage;
        vm.formItem.notice_content = data.notice_content;
        vm.formItem.recommendDesc = JSON.parse(data.recommend_desc);
        vm.formItem.category = data.category_id;
        vm.formItem.init_views = data.init_views;
        vm.formItem.title = data.title;
        vm.formItem.show = data.show;
        vm.formItem.pay_type = data.pay_type;
        vm.formItem.transport_type = data.transport_type;
        // vm.formItem.original_price = data.original_price;
        // vm.formItem.lowest_price = data.lowest_price;
        vm.formItem.sku = data.sku;
        vm.formItem.sku.forEach((v, index) => {
          v.original_price = vm.GLOBAL.convertPrice(v.original_price);
          v.lowest_price = vm.GLOBAL.convertPrice(v.lowest_price);
          v.vip_discount = vm.GLOBAL.convertPrice(v.vip_discount);
          v.reward = vm.GLOBAL.convertPrice(v.reward);
          try {
            if (vm.formItem.type === vm.GLOBAL.ACTIVE_TYPE_KJ) {
              v.rule_data.rule = JSON.parse(v.rule_data.rule);
              if (v.rule_data.rule != null && v.rule_data.rule.length > 0) {
                v.rule_data.rule.forEach(rule => {
                  rule.price = vm.GLOBAL.convertPrice(rule.price);
                  rule.start_price = vm.GLOBAL.convertPrice(rule.start_price);
                  rule.end_price = vm.GLOBAL.convertPrice(rule.end_price);
                })
              }
            }
          } catch (e) {
          }

        });
        vm.formItem.limit_buy_count = data.limit_buy_count;
        vm.formItem.date = [data.start_time, data.end_time];
        vm.formItem.use_limit_date = [data.start_use_time, data.end_use_time];

        try {
          vm.formItem.banners = JSON.parse(data.banners);
          vm.formItem.video = JSON.parse(data.video);
        } catch (e) {
          console.log(e);
        }
        vm.formItem.share_title = data.share_title;
        vm.formItem.share_content = data.share_content;
        try {
          vm.formItem.share_img = JSON.parse(data.share_img);
          vm.formItem.share_img_bg = JSON.parse(data.share_img_bg);
        } catch (e) {
          console.log(e);
        }
        vm.formItem.content = data.content;
        vm.formItem.region = [];
        if (data.limit_province !== null && data.limit_province !== '' && data.limit_province !== undefined) {
          vm.formItem.region.push(data.limit_province);
        }
        if (data.limit_city !== null && data.limit_city !== '' && data.limit_city !== undefined) {
          vm.formItem.region.push(data.limit_city);
        }
        if (data.limit_district !== null && data.limit_district !== '' && data.limit_district !== undefined) {
          vm.formItem.region.push(data.limit_district);
        }
      },
      submit() {
        let vm = this;
        vm.$refs['myForm'].validate((valid) => {
          if (valid) {
            vm.submitLoading = true;
            let skus = [];
            vm.formItem.sku.forEach((v, index) => {
              let item = JSON.parse(JSON.stringify(v));
              item.original_price = vm.GLOBAL.reconvertPrice(item.original_price);
              item.lowest_price = vm.GLOBAL.reconvertPrice(item.lowest_price);
              item.vip_discount = vm.GLOBAL.reconvertPrice(item.vip_discount);
              item.reward = vm.GLOBAL.reconvertPrice(item.reward);
              if (vm.formItem.type === vm.GLOBAL.ACTIVE_TYPE_KJ) {
                if (item.rule_data.rule != null && item.rule_data.rule.length > 0) {
                  item.rule_data.rule.forEach(v => {
                    v.price = vm.GLOBAL.reconvertPrice(v.price);
                    v.start_price = vm.GLOBAL.reconvertPrice(v.start_price);
                    v.end_price = vm.GLOBAL.reconvertPrice(v.end_price);
                  })
                }
              }
              skus.push(item);
            });
            let params = {
              id: vm.formItem.id,
              sid: vm.formItem.sid,
              distribution_type: vm.formItem.distribution_type,
              distribution_money: vm.formItem.distribution_money,
              distribution_percentage: vm.formItem.distribution_percentage,
              refund_percentage: vm.formItem.refund_percentage,
              type: vm.formItem.type,
              recommend_desc: JSON.stringify(vm.formItem.recommendDesc),
              transport_type: vm.formItem.transport_type,
              pay_type: vm.formItem.pay_type,
              category_id: vm.formItem.category,
              title: vm.formItem.title,
              show: vm.formItem.show,
              notice_content: vm.formItem.notice_content,
              init_views: vm.formItem.init_views,
              sku: skus,
              start_time: vm.GLOBAL.convertUTCTimeToLocalTime(vm.formItem.date[0]),
              end_time: vm.GLOBAL.convertUTCTimeToLocalTime(vm.formItem.date[1]),
              banners: JSON.stringify(vm.formItem.banners),
              video: JSON.stringify(vm.formItem.video),
              start_use_time: vm.GLOBAL.convertUTCTimeToLocalTime(vm.formItem.use_limit_date[0]),
              end_use_time: vm.GLOBAL.convertUTCTimeToLocalTime(vm.formItem.use_limit_date[1]),
              share_title: vm.formItem.share_title,
              share_content: vm.formItem.share_content,
              share_img: JSON.stringify(vm.formItem.share_img),
              share_img_bg: JSON.stringify(vm.formItem.share_img_bg),
              content: vm.formItem.content,
              limit_province: vm.formItem.region[0] === undefined ? null : vm.formItem.region[0].code,
              limit_city: vm.formItem.region[1] === undefined ? null : vm.formItem.region[1].code,
              limit_district: vm.formItem.region[2] === undefined ? null : vm.formItem.region[2].code
            };
            save(params).then(response => {
              vm.submitLoading = false;
              vm.$Message.success(response.data.msg);
              vm.showOnly = true;
            }, (error) => {
              vm.submitLoading = false;
            });
          }
        });
      },
      doCancel() {
      },
      bannerImgSelected(imgs) {
        this.formItem.banners = imgs;
      },
      videoSelected(video) {
        this.formItem.video = video;
      },
      shareImgSelected(imgs) {
        this.formItem.share_img = imgs;
      },
      shareImgBgSelected(imgs) {
        this.formItem.share_img_bg = imgs;
      },
      preview() {
      }
    }
  }
</script>
